﻿using Bussiness.DEV;
using HslCommunication;
using HslCommunication.Profinet.Melsec;
using HslCommunication.Profinet.Omron;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bussiness
{
    public class OMRON_PLC : IDEV
    {
        public Device Device { get; set; }
        public List<DevicePOI> DevicePOIList { get; set; }
        OmronFinsNet _omronFinsNet;

        public Result Connect()
        {
            Result result = new Result();
            _omronFinsNet= new OmronFinsNet(Device.IP, Device.Port.Value);
            OperateResult operate = _omronFinsNet.ConnectServer();
            result.Success = operate.IsSuccess;
            result.Msg = operate.Message;
            return result;
        }

        public Result<List<Cfg_Field>> Read()
        {
            Result<List<Cfg_Field>> result = new Result<List<Cfg_Field>>();
            foreach (var devPOI in DevicePOIList)
            {
                Cfg_Field cfg_Field = new Cfg_Field();
                cfg_Field.name = devPOI.DevPOIAlias;

                switch ((DevPOIType)devPOI.DevPOIType)
                {
                    case DevPOIType.data_int:
                        var resultInt = _omronFinsNet.ReadInt32(devPOI.Address);
                        if (resultInt.IsSuccess)
                        {
                            result.Success = true;
                            cfg_Field.value = resultInt.Content.ToString();
                        }
                        break;
                    case DevPOIType.data_short:
                        var resultShort = _omronFinsNet.ReadUInt16(devPOI.Address);
                        if (resultShort.IsSuccess)
                        {
                            result.Success = true;
                            cfg_Field.value = resultShort.Content.ToString();
                        }
                        break;
                    case DevPOIType.data_long:
                        var resultLong = _omronFinsNet.ReadInt32(devPOI.Address);
                        if (resultLong.IsSuccess)
                        {
                            result.Success = true;
                            cfg_Field.value = resultLong.Content.ToString();
                        }
                        break;
                    case DevPOIType.data_float:
                        var resultFloat = _omronFinsNet.ReadFloat(devPOI.Address);
                        if (resultFloat.IsSuccess)
                        {
                            result.Success = true;
                            cfg_Field.value = resultFloat.Content.ToString();
                        }
                        break;
                    case DevPOIType.data_string:
                        string[] arr = devPOI.Address.Split('_');
                        ushort _length = Convert.ToUInt16(arr[1]);
                        var resultString = _omronFinsNet.ReadString(devPOI.Address, _length);
                        if (resultString.IsSuccess)
                        {
                            result.Success = true;
                            cfg_Field.value = resultString.Content.ToString();
                        }
                        break;
                    case DevPOIType.data_bit:
                        var resultBool = _omronFinsNet.ReadBool(devPOI.Address);
                        if (resultBool.IsSuccess)
                        {
                            result.Success = true;
                            cfg_Field.value = resultBool.Content.ToString();
                        }
                        break;
                }

                result.Data.Add(cfg_Field);
            }

            return result;
        }
    }
}
